<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>2018/12/4</title>
    <script src="node_modules/_vue@2.5.17@vue/dist/vue.min.js"></script>
    <script src="node_modules/_mockjs@1.0.1-beta3@mockjs/dist/mock-min.js"></script>
    <script src="node_modules/jquery/dist/jquery.min.js"></script>

    <!--好的代码像粥一样,是得用时间和精力熬的。-->
</head>
<body>

</body>
<script>
    mock(/http:\/\/a\.com?(\/*.)/,  function(options){
        var count = options.params.count;
         return this.mock({
            ['data|10' + count]: [{
                name: '@name',
                city: '@city'
            }]
        })
    });
    $.ajax({
        method: 'get',
        url: 'http://a.com',
        dataType: 'json',
        data: {
          count: 10
       }
    })
       .then(function(data){
           console.log(data);
       })
       .catch(function(error){
        console.log(error);
    });

    function mock (url, callback, config) {
        config && Mock.setup(config);
        Mock.mock(url, function (options) {
            var op = {
                get: function() {
                    var params = options.url.split('?')[1].split('&').reduce(function(pre, current){
                        var arr = current.split('=');
                        pre[arr[0]] = arr[1];
                        return pre;
                    }, {});
                    options.method = options.type;
                    options.url = options.url.split('?')[0];
                    options.params = params;
                    delete options.type;
                    delete options.body;
                },
                post: function () {
                    var body = options.body.split('?')[1].split('&').reduce(function(pre, current){
                        var arr = current.split('=');
                        pre[arr[0]] = arr[1];
                        return pre;
                    }, {});
                    options.method = options.type;
                    options.body = body;
                    delete options.type;
               }
            };
            op[options.type.toLowerCase()]();
            return callback.call(Mock, options);
           /*if(options.type.toLowerCase() === 'get'){
               var parmas = options.url.split("?")[1].split('&').reduce(function(pre, current){
                   var arr = current.split('=');
                   pre[arr[0]] = arr[1];
                   return pre;
               }, {});
               options.method = options.type;
               options.url = options.url.split("?")[0];
               options.parmas = parmas;
               delete  options.body;
               delete  options.type;
           } else if(options.type.toLowerCase() === 'post'){
                var body = options.body.split("?")[1].split('&').reduce(function(pre, current){
                    var arr = current.split('=');
                    pre[arr[0]] = arr[1];
                    return pre;
                }, {});
                options.method = options.type;
                options.body = body;
                delete options.type;
            }*/
        })
    }
</script>
</html>